﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Windows.Forms;
using System.Text;
using System.Threading;
using TouchUI;

namespace Sample
{
    public class DetailScrollPanel : ScrollPanel
    {
        private int productNo;
        private Bitmap[] images = new Bitmap[2];
        private int imageHeight;
        private int currentImage;
        private int animationOffset;
        private int animationLeft;

        public DetailScrollPanel(int productNo, int screenFactor, Rectangle r)
        {
            this.productNo = productNo;
            this.images[0] = Common.Instance.LoadBitmap("image", 240, 180);
            this.images[1] = Common.Instance.LoadBitmap("imagex", 240, 180);
            ScreenFactor = screenFactor;
            Rectangle = r;
            imageHeight = Rectangle.Width * images[0].Size.Height / images[0].Size.Width;
            ScrollHeight = 600 * screenFactor;
        }

        public override void Paint(Graphics g)
        {
            base.Paint(g);

            imageHeight = Rectangle.Width * images[0].Size.Height / images[0].Size.Width;

            if(animationLeft != 0)
            {
                g.DrawImage(images[currentImage - animationLeft.CompareTo(0)], new Rectangle(animationLeft - Rectangle.Width * animationLeft.CompareTo(0),
                    Rectangle.Top + ScrollTop, Rectangle.Width, this.imageHeight),
                    new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);
                g.DrawImage(images[currentImage], new Rectangle(animationLeft, Rectangle.Top + ScrollTop, Rectangle.Width, this.imageHeight),
                    new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);
                animationLeft -= animationOffset;
                if(Math.Abs(animationLeft) < Math.Abs(animationOffset))
                    animationLeft = 0;
            }
            else
                g.DrawImage(images[currentImage], new Rectangle(Rectangle.Left, Rectangle.Top + ScrollTop, Rectangle.Width, imageHeight),
                    new Rectangle(0, 0, images[0].Width, images[0].Height), GraphicsUnit.Pixel);

            string s = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor " +
                "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud " +
                "exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure " +
                "dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " +
                "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. ";

            g.DrawString(s + s, Common.Instance.Font, Common.Instance.FontBrush, new RectangleF(Rectangle.Left + 8 * ScreenFactor,
                Rectangle.Top + ScrollTop + imageHeight + 8, Rectangle.Width - 16 * ScreenFactor, ScrollHeight - imageHeight - 16 * ScreenFactor),
                new StringFormat());
        }

        public override void MouseUp(Point p)
        {
            base.MouseUp(p);

            if(HorizontalGesture != 0 && p.Y < imageHeight)
            {
                if(HorizontalGesture > 0 && currentImage < 1)
                {
                    animationOffset = Rectangle.Width / 4;
                    animationLeft = Rectangle.Width;
                    currentImage++;
                }
                else if(HorizontalGesture < 0 && currentImage > 0)
                {
                    animationOffset = -Rectangle.Width / 4;
                    animationLeft = -Rectangle.Width;
                    currentImage--;
                }
            }
        }

        public override void KeyUp(KeyEventArgs e)
        {
            base.KeyUp(e);

            if(e.KeyCode == Keys.Up)
            {
                ScrollTop += Rectangle.Height / 4;
                if(ScrollTop > 0)
                    ScrollTop = 0;
            }
            else if(e.KeyCode == Keys.Down)
            {
                ScrollTop -= Rectangle.Height / 4;
                if(ScrollTop < Rectangle.Height - Rectangle.Top - ScrollHeight)
                    ScrollTop = Rectangle.Height - Rectangle.Top - ScrollHeight;
            }
            else if(e.KeyCode == Keys.Right && currentImage < 1)
            {
                animationOffset = Rectangle.Width / 4;
                animationLeft = Rectangle.Width;
                currentImage++;
            }
            else if(e.KeyCode == Keys.Left && currentImage > 0)
            {
                animationOffset = -Rectangle.Width / 4;
                animationLeft = -Rectangle.Width;
                currentImage--;
            }
            e.Handled = true;
        }
    }
}
